<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="用户id" prop="userId">
        <el-input v-model="queryParams.userId" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="queryParams.userName" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="车辆id" prop="vehicleId">
        <el-input v-model="queryParams.vehicleId" placeholder="请输入车辆id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="车辆品牌和型号" prop="brandModel">
        <el-input v-model="queryParams.brandModel" placeholder="请输入车辆品牌和型号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="租赁开始时间" prop="startTime">
        <el-date-picker clearable v-model="queryParams.startTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择租赁开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="租赁天数" prop="contiTime">
        <el-input v-model="queryParams.contiTime" placeholder="请输入租赁天数" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="总租金" prop="totalCost">
        <el-input v-model="queryParams.totalCost" placeholder="请输入总租金" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="支付状态" prop="paymentStatus">
        <el-select v-model="queryParams.paymentStatus" placeholder="请选择支付状态" clearable>
          <el-option v-for="dict in dict.type.rental_sys_orders_payment_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option v-for="dict in dict.type.rental_sys_orders_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="ordersYongList" @selection-change="handleSelectionChange">
      <el-table-column label="订单id" align="center" prop="orderId" width="80" />
      <!--      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="用户名" align="center" prop="userName" /> -->
      <el-table-column label="车辆id" align="center" prop="vehicleId" width="80" />
      <el-table-column label="车辆品牌和型号" align="center" prop="brandModel" width="150" />
      <el-table-column label="租赁开始时间" align="center" prop="startTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租赁天数" align="center" prop="contiTime" width="80" />
      <el-table-column label="总租金" align="center" prop="totalCost" width="80" />
      <el-table-column label="支付状态" align="center" prop="paymentStatus" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rental_sys_orders_payment_status" :value="scope.row.paymentStatus" />
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.rental_sys_orders_status" :value="scope.row.orderStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div v-if="scope.row.orderStatus === '00'">
            请等待业务员审核订单
          </div>
          <div v-else>
            <el-button v-if="scope.row.orderStatus === '01'" size="mini" type="text" icon="el-icon-goods"
              @click="handleUpdate(scope.row)" v-hasPermi="['system:paymentRecords:add']" 去支付 </el-button>
              <el-button v-if="scope.row.orderStatus === '01'" size="mini" type="text" icon="el-icon-goods"
                @click="handleUpdate(scope.row)"
                            v-hasPermi="['system:ordersYong:edit']"
                取消订单
              </el-button>
              <el-button v-if="scope.row.orderStatus === '10'" size="mini" type="text" icon="el-icon-return"
                @click="handleReturnVehicle(scope.row)" v-hasPermi="['some:permission:to:return:vehicle']">
                去归还
              </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改我的订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="车辆id" prop="vehicleId">
          <el-input v-model="form.vehicleId" placeholder="请输入车辆id" />
        </el-form-item>
        <el-form-item label="车辆品牌和型号" prop="brandModel">
          <el-input v-model="form.brandModel" placeholder="请输入车辆品牌和型号" />
        </el-form-item>
        <el-form-item label="租赁开始时间" prop="startTime">
          <el-date-picker clearable v-model="form.startTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择租赁开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="租赁天数" prop="contiTime">
          <el-input v-model="form.contiTime" placeholder="请输入租赁天数" />
        </el-form-item>
        <el-form-item label="总租金" prop="totalCost">
          <el-input v-model="form.totalCost" placeholder="请输入总租金" />
        </el-form-item>
        <el-form-item label="支付状态" prop="paymentStatus">
          <el-select v-model="form.paymentStatus" placeholder="请选择支付状态">
            <el-option v-for="dict in dict.type.rental_sys_orders_payment_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-select v-model="form.orderStatus" placeholder="请选择订单状态">
            <el-option v-for="dict in dict.type.rental_sys_orders_status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listOrdersYong,
    getOrdersYong,
  } from "@/api/system/ordersYong";
  import store from "@/store";
  import { updateOrderStatus } from "@/api/system/ordersYong";
  export default {
    name: "OrdersYong",
    dicts: ['rental_sys_orders_payment_status', 'rental_sys_orders_status'],
    data() {
      return {
        userId: store.state.userId,
        userName: store.state.userName,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 我的订单表格数据
        ordersYongList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          userId: null,
          userName: null,
          pageNum: 1,
          pageSize: 10,
          vehicleId: null,
          brandModel: null,
          startTime: null,
          contiTime: null,
          totalCost: null,
          paymentStatus: null,
          orderStatus: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          userId: [{
            required: true,
            message: "用户id不能为空",
            trigger: "blur"
          }],
          userName: [{
            required: true,
            message: "用户名不能为空",
            trigger: "blur"
          }],
          vehicleId: [{
            required: true,
            message: "车辆id不能为空",
            trigger: "blur"
          }],
          startTime: [{
            required: true,
            message: "租赁开始时间不能为空",
            trigger: "blur"
          }],
          contiTime: [{
            required: true,
            message: "租赁天数不能为空",
            trigger: "blur"
          }],
          totalCost: [{
            required: true,
            message: "总租金不能为空",
            trigger: "blur"
          }],
          paymentStatus: [{
            required: true,
            message: "支付状态不能为空",
            trigger: "change"
          }],
          orderStatus: [{
            required: true,
            message: "订单状态不能为空",
            trigger: "change"
          }]
        }
      };
    },
    created() {
      this.userId = this.$store.state.user.id;
      this.userName = this.$store.state.user.name;
      this.queryParams.userId = this.userId;
      this.queryParams.userName = this.userName;
      this.getList();
    },
    methods: {
      /** 查询我的订单列表 */
      getList() {
        this.loading = true;
        listOrdersYong(this.queryParams).then(response => {
          this.ordersYongList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        // 重新从Vuex store中获取用户ID和用户名
        this.userId = this.$store.state.user.id;
        this.userName = this.$store.state.user.name;

        this.form = {
          orderId: null,
          userId: null,
          userName: null,
          vehicleId: null,
          brandModel: null,
          startTime: null,
          contiTime: null,
          totalCost: null,
          paymentStatus: null,
          orderStatus: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$refs.queryForm.resetFields(); // 使用表单引用重置表单
        this.handleQuery();
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.orderId != null) {
              updateOrdersYong(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addOrdersYong(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
handleCancelOrder(row) {
  this.$confirm('确认要取消该订单吗?', '提示', {
    // ... 其他选项 ...
  }).then(() => {
    updateOrderStatus(row.orderId, '20').then(response => { // 使用新的API方法
      // 在这里添加提示信息
      this.$message({
        type: 'success',
        message: '你的租赁订单取消成功'
      });
      // 刷新列表或执行其他操作
      this.getList();
    }).catch(error => {
      // 处理错误
      this.$message.error('取消订单失败: ' + error.message);
    });
  }).catch(() => {
    // 用户点击了取消按钮，不执行任何操作
  });
}


    }
  };
</script>
